package com.shelpe.services.scheduler.po;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import com.shelpe.services.scheduler.consts.Device;
import com.shelpe.services.scheduler.consts.Location;
import com.shelpe.services.scheduler.dto.InsightWordData;

import lombok.Data;

@Document(collection="word_insight_metrics")
@Data
public class WordInsightMetric extends BaseEntity {

	@Id
	private String metricID;
	@Indexed
	private List<Long> keywordIDs;
	@Indexed
	private List<String> adgroupRecordIDs;
	@Indexed
	private List<Long> adgroupIDs;
	@Indexed
	private String word;
	private Scope scope;
	
	private boolean global;
	private Device device = Device.all;//设备
	private Location location = Location.all;
	private String categoryID;
	private String province;
	private String city;
	
	private InsightMetrics metric;
	
	public WordInsightMetric(){}
	
	public void addAdgroupID(long adgroupID){
		if(this.adgroupIDs == null){
			this.adgroupIDs = new ArrayList<Long>();
		}
		if(!this.adgroupIDs.contains(adgroupID)){
			this.adgroupIDs.add(adgroupID);
		}
	}
	
	public void addAdgroupRecordID(String recordID){
		if(this.adgroupRecordIDs == null){
			this.adgroupRecordIDs = new ArrayList<String>();
		}
		if(!this.adgroupRecordIDs.contains(recordID)){
			this.adgroupRecordIDs.add(recordID);
		}
	}
	
	public void addKeywordID(long keywordID){
		if(this.keywordIDs == null){
			this.keywordIDs = new ArrayList<Long>();
		}
		if(!this.keywordIDs.contains(keywordID)){
			this.keywordIDs.add(keywordID);
		}
	}
	
	public void from(InsightWordData data, Scope scope){
		 this.metric = new InsightMetrics();
		 BeanUtils.copyProperties(data, this.metric);
		 BigDecimal bd = BigDecimal.valueOf(NumberUtils.toDouble(data.getCtr()));
		 this.metric.setCtr(bd);
		 bd = BigDecimal.valueOf(NumberUtils.toDouble(data.getCpc()));
		 this.metric.setCpc(bd);
		 bd = BigDecimal.valueOf(NumberUtils.toDouble(data.getCpc()));
		 this.metric.setRoi(bd);
		 bd = BigDecimal.valueOf(NumberUtils.toDouble(data.getCpc()));
		 this.metric.setCoverage(bd);
		 
		 this.setWord(data.getBidword());
		 
		 if(StringUtils.isNotBlank(data.getCategoryID())){
			 this.categoryID = data.getCategoryID();
		 }else if(data.getNetwork() > 0){
			 if(data.getNetwork() == InsightWordData.NETWORK_MOBILE_EXTERNAL || data.getNetwork() == InsightWordData.NETWORK_MOBILE_INTERNAL){
				 device = Device.mobile;
			 }
			 if(data.getNetwork() == InsightWordData.NETWORK_PC_EXTERNAL || data.getNetwork() == InsightWordData.NETWORK_PC_INTERNAL){
				 device = Device.pc;
			 }
		 }else{
			 this.global = true;
		 }
		 
		 this.scope = scope;
	}
	
	public void setupMetricID(){
		String ext = "g";
		if(this.device != Device.all || this.location != Location.all){
			ext = "d[" + this.device.name() + "]" + "l[" + this.location.name() + "]";
		}else if(this.categoryID != null){
			ext = "c[" + this.categoryID + "]";
		}else if(this.province != null || this.city != null){
			ext = "p[" + this.province + "]" + "ct[" + this.city + "]";
		}
		this.metricID = WordInsightMetric.calcMetricID(word, scope, ext);
	}
	
	public static String calcMetricID(String word, Scope scope, String ext){
		StringBuffer buffer = new StringBuffer();
		buffer.append(word).append("_");
		buffer.append(scope.toIdentity()).append("_");
		buffer.append(ext);
		return buffer.toString();
	}
}
